package cn.org.eagle_framework.generator;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;

import cn.org.eagle_framework.generator.provider.db.DataSourceProvider;
import cn.org.eagle_framework.generator.provider.db.table.model.Column;
import cn.org.eagle_framework.generator.provider.db.table.model.Table;

/**
 * 用于处理自定义的业务
 * @author HYH
 */
public class GeneratorBiz {
	public static String bizName="";
	public static String bizNameCode="";
	private String basepackage;//基础包名
	private String[] modelNames;
	private String className;
	public String getBasepackage() {
		return basepackage;
	}

	public void setBasepackage(String basepackage) {
		this.basepackage = basepackage;
	}

	public GeneratorBiz() {
	}
	

	public String[] getModelNames() {
		return modelNames;
	}

	public void setModelNames(String[] modelNames) {
		this.modelNames = modelNames;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	/**
	 * 用于提取基类Vo的名称
	 * @param templateModel
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void dealClassName(Map filePathModel,Map templateModel){
		String curStr=getModelNames()[getModelNames().length-1];
		this.setClassName(curStr.substring(0, 1).toUpperCase()+curStr.substring(1, curStr.length()).toLowerCase());
		filePathModel.put("className",this.getClassName());
		Table table=(Table) templateModel.get("table");
		table.setClassName(this.getClassName());
		templateModel.put("table", table);
		templateModel.put("bizNameCode", GeneratorBiz.bizNameCode);
	}
	
	
	
	/**
	 * 用于提取基类Vo的名称
	 * @param templateModel
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void dealBaseVoName(Map templateModel){
		String baseVoPath=templateModel.get("BaseVo").toString();
		String[] baseVoPaths=baseVoPath.split("\\.");
		templateModel.put("baseVoPath",baseVoPaths[baseVoPaths.length-1]);
	}
	
	/**
	 * 用于提取基类Action的名称
	 * @param templateModel
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void dealBaseActionName(Map templateModel){
		String baseActionPath=templateModel.get("BaseAction").toString();
		String[] baseActionPaths=baseActionPath.split("\\.");
		templateModel.put("baseActionPath",baseActionPaths[baseActionPaths.length-1]);
	}
	
	/**
	 * 用于提取基类Service的名称
	 * @param templateModel
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void dealBaseServiceName(Map templateModel){
		String baseServicePath=templateModel.get("BaseService").toString();
		String[] baseServicePaths=baseServicePath.split("\\.");
		templateModel.put("BaseServicePath",baseServicePaths[baseServicePaths.length-1]);
	}
	
	/**
	 * 用于提取基类Dao的名称
	 * @param templateModel
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void dealBaseDaoName(Map templateModel){
		String baseDaoPath=templateModel.get("BaseDao").toString();
		String[] baseDaoPaths=baseDaoPath.split("\\.");
		templateModel.put("BaseDaoPath",baseDaoPaths[baseDaoPaths.length-1]);
	}
	
	/**
	 * 定义jsp的路径
	 * @param templateModel
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void dealJspPath(Map templateModel){
		// 将包路径转化为jsp路径
		String basepackage = templateModel.get("basepackage") == null ? "": templateModel.get("basepackage").toString();
		this.setBasepackage(basepackage);
		Table table=(Table)templateModel.get("table");
		if (!"".equals(basepackage)) {
			String jsppath = basepackage.replaceAll("\\.", "/");
			templateModel.put("jsppath", jsppath+"/"+table.getClassName().toLowerCase());
		}
	}
	
	/**
	 * 处理前缀
	 * @param templateModel
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void dealPrefix(Map templateModel){
		String basepackage = templateModel.get("basepackage") == null ? "": templateModel.get("basepackage").toString();
		basepackage = basepackage == null ? "" : basepackage;
		String[] packs = basepackage.split("\\.");
		if (packs.length >= 2) {
			templateModel.put("prefix", packs[packs.length-1]);
		}
	}
	
	/**
	 * 处理业务包名
	 * @param filePathModel
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void dealBizpackage(Map filePathModel){
		filePathModel.put("bizpackage",getModelNames()[getModelNames().length-1].toLowerCase());
	
	}
	
	
	/**
	 * 处理业务包名
	 * @param filePathModel
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void dealbasepackage(Map filePathModel){
		if(this.getModelNames().length>1){
			for (int i = 0; i < this.getModelNames().length-1; i++) {
				String modelName = this.getModelNames()[i];
				filePathModel.put("basepackage",filePathModel.get("basepackage").toString().toLowerCase()+"."+modelName);
			}
		}
		
	
	}
	
	/**
	 * 处理业务包名
	 * @param filePathModel
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void dealbasepackagedir(Map filePathModel){
		String sqlName="";
		String curStr="";
		if(getModelNames()==null||getModelNames().length==0){
			sqlName=filePathModel.get("sqlName").toString().toLowerCase();
			String[] tableModels=sqlName.split("_");
			this.setModelNames(tableModels);
			if(tableModels.length>1){
				for (int i = 0; i < tableModels.length-1; i++) {
					String modelName = tableModels[i];
					filePathModel.put("basepackage_dir",filePathModel.get("basepackage_dir").toString().toLowerCase()+"/"+modelName);
				}
			}
		}
	}
	
	
	
	public static void main(String[] args) {
		System.out.println("MAR_SYS_TIME".split("_").length);;
	}
	
	
	/**
	 * 通过sql查询出不同数据库的备注
	 * @param templateModel
	 */
	@SuppressWarnings("rawtypes")
	public  void dealColumnComment(Map templateModel) {
		Table table = (Table) templateModel.get("table");
		Object commentsql = templateModel.get("comment_sql");
		if (commentsql != null && !"".equals(commentsql.toString())) {

			Connection con = DataSourceProvider.getConnection();
			Map<String, String> map = new HashMap<String, String>();
			try {
				Statement stat = con.createStatement();
				ResultSet rs = stat.executeQuery(commentsql.toString().replace("$tableName$", table.getSqlName()));
				while (rs.next()) {
					String columnName = rs.getString("columnName");
					String remarks = rs.getString("remarks");
					map.put(columnName, remarks);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			Object[] columns = table.getColumns().toArray();
			LinkedHashSet<Column> linkedColumns=new LinkedHashSet<Column>();
			for (int i = 0; i < columns.length; i++) {
				Column column = (Column) columns[i];
				column.setRemarks(map.get(column.getSqlName()));
				linkedColumns.add(column);
			}
			
			table.setColumns(linkedColumns);
			templateModel.put("table", table);
		}

	}
}
